var th;//操作目标
var adsType;//操作类别



//展开/收缩收货地址栏
function actionAddressBar(){
    $('#div_AddressSet').toggle();
}

 //顶部提示 
 //state:(1)删除  (2)设置默认值  (3)添加新地址 (4)内容为空
function tipMsg($state,$type){
    var content;
    switch ($state) {
        case 1:
            content="删除成功!";
            break;
        case 2:
            content="设置成功!";
            break;
        case 3:
            content="添加成功!";
            break;
        case 4:
            content="内容为空添加失败!";
            break;
        case 5:
            content="删除失败!";
            break;
        case 6:
            content="手机号码格式不正确!";
            break;
    }
    var el;
    el=$.tips({
        content:content,
        stayTime:1300,
        type:$type
    })
    
}

//弹出底部选择
function popAction($this){
     $('.ui-actionsheet').addClass('show').show();
     $th=$this;
} 

//删除地址
function removeAddress(){
    $.post(removeUrl, {InfoId:$($th).parent().attr('id')},
        function(response){
            if(response!=0){//判断是否删除成功
                $($th).parent().remove();
                tipMsg(1,"success");
            }else{
                tipMsg(5,"warn");
            }
        },'json'
    );
    closeMsg();
}


//设置默认地址
function setDefaultAddress(){
    var temp=$($th).parent().find('i');
    var aClass=temp.attr("class")==""?"ui-icon-checked-s":"";
    var old_th=$th;
    $th=$('.ui-icon-checked-s');
    defaultValue();
    if($th!=old_th && $th.length!=0)
        actionAds();
    $th=old_th;
    temp.attr('class',aClass);
    tipMsg(2,"success");    
    closeMsg();
    actionAds();
}

//清理默认值
function defaultValue(){
   $('#div_AddressSet').find('i').attr('class','');
}

//取消提示
function closeMsg(){
    $('.ui-actionsheet').addClass('show').hide();
    
}

//按钮点击 设置状态为新建
function addNewAddress(){
    
    $adsType=1;
    $('.ui-dialog').addClass('show').show();
}

//操作地址 配合adsType变量确定是添加还是修改
function actionAddress(){
    var adsN=$('#adsName').val();
    var adsP=$('#adsPhone').val();
    var adsA=$('#adsAddress').val();
    var myreg = /^1[3458]\d{9}$/;
    if(adsN=="" || adsP=="" || adsA==""){
        $('.ui-dialog').addClass('show').hide();
        tipMsg(4,"warn");
        return; 
    }else if(!myreg.exec(adsP)){
        tipMsg(6,"warn");
        return; 
    }
    
    //添加 li元素
    if($adsType==1){
       $('#div_AddressSet').find('ul').prepend(
        "<li class='ui-border-t' id=''>"+
        "<div class='ui-list-info' onclick='popAction(this);'>"+
        "<p class='ui-nowrap'>"+adsN+"&&"+adsP+"</p>"+
        "<p class='ui-nowrap'>"+adsA+"</p>"+
        "</div>"+
        "<i class=''   style='font-size:27px;'></i>"+
        "</li>"
        );
        tipMsg(3,"success");
        var temp=$('#div_AddressSet').find('ul').find('li');
        $th=temp[0];
    }else if($adsType==2){//修改元素
        var temp=$($th).parent().find('p');
        temp[0].innerHTML=adsN+"&&"+adsP;
        temp[1].innerHTML=adsA;
        tipMsg(2,"success");
    }
    $('.ui-dialog').addClass('show').hide();
    clearAddress();
    actionAds();
}

//清理添加内容
function clearAddress(){
    $('#adsName').val('');
    $('#adsPhone').val('');
    $('#adsAddress').val('');
}

//按钮点击 填充要修改的内容，并设置状态为修改
function updateAddresss(){
    closeMsg();//取消提示
    $adsType=2;//修改状态
    var temp=$($th).parent().find('p');
    var adsN=temp[0].innerHTML.split('&amp;&amp;');
    var adsA=temp[1].innerHTML;
    $('#adsName').val(adsN[0]);
    $('#adsPhone').val(adsN[1]);
    $('#adsAddress').val(adsA);
    $('.ui-dialog').addClass('show').show();
}

//添加、修改地址
function actionAds(){
    var temp=$($th).parent().find('p');
    var adsId=$($th).parent().attr('id');
    var uId=1;
    var state;
    adsId=(adsId==""?"null":adsId);
    //alert('id:'+adsId);
    var adsN=temp[0].innerHTML.split('&amp;&amp;');
    var adsA=temp[1].innerHTML;
    var adsD=$($th).parent().find('i').attr('class');
    if(adsType==1){//清理默认值
       state = adsType;
    }
    $.post(actionUrl, {isUserId:uId,InfoId:adsId,TakeName:adsN[0],TakePhone:adsN[1],TakeAddress:adsA,YnDefault:adsD,isState:state},
        function(response){
            if(adsId=='null'){
                //console.log($th);
                $($th).attr('id',response);
            }
        },'json'
    );
    
}









    
    



